#include "GameplayBackground.h"
#include "Iw2D.h"
#include "il_assert.h"

bool check_collision_top_level1_shore_top( const Ship &ship )
{
	if( ship.GetPos().y < (34 + 25) ) {
		return true;
	}
	
	return false;
}

bool check_collision_top_level1_shore_bottom( const Ship &ship )
{
	if( ship.GetPos().x <= (274 + 25) && ship.GetPos().y >= 280 - 25 ) {
		return true;
	}
	
	return false;
}

bool check_collision_top_level5_shore_top( const Ship &ship )
{
	if( ship.GetPos().y < (92 + 25) ) {
		return true;
	}

	return false;
}

bool check_collision_top_level5_shore_bottom( const Ship &ship )
{
	if( ship.GetPos().y >= 179 - 25 ) {
		return true;
	}

	return false;
}

bool check_collision_top_level6_shore_top( const Ship &ship )
{
	if( ship.GetPos().y < (115 + 25) ) {
		return true;
	}

	return false;
}

bool check_collision_top_level6_shore_bottom( const Ship &ship )
{
	if( ship.GetPos().y >= (320-75) - 25 ) {
		return true;
	}

	return false;
}

void GameplayBackground::Load( const char* top_bg, const char* bottom_bg ) 
{
	m_bg_bottom.Load( bottom_bg );
	m_bg_top.Load( top_bg );
	
	m_num_top_points = 0;
	m_num_collision_objects = 0;
	
	if( strcmp(top_bg, "level1_shore_bg_top") == 0 ) {
		// setup the points for the collision sprite
		
		m_top_shore_collision_pre_check = check_collision_top_level1_shore_top;
		
		m_bg_top_pos = CIwSVec2(0,0);
		
		// top
		m_top_collision_points[m_num_top_points] = CIwFVec2( 0, 26 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 21, 18 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 42, 25 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 192, 27 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 218, 36 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 240, 36 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 274, 26 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 292, 33 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 444, 34 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 464, 26 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 480, 29 );
		++m_num_top_points;
	} else if( strcmp(top_bg, "level5_shore_bg_top") == 0 ) {
		m_bg_top_pos = CIwSVec2(0,0);
		m_top_shore_collision_pre_check = check_collision_top_level5_shore_top;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 0, 40 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 15, 30 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 35, 32 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 72, 27 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 113, 26);
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 138, 33 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 193, 34);
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 228, 39 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 261, 39 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 275, 34 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 381, 40 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 425, 44 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 445, 55 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 477, 88 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 485, 89 );
		++m_num_top_points;
	} else if( strcmp(top_bg, "level6_shore_bg_top") == 0 ) {
		m_bg_top_pos = CIwSVec2(0,0);
		m_top_shore_collision_pre_check = check_collision_top_level6_shore_top;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 0, 32 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 38, 48 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 53, 48 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 72, 57 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 102, 105 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 122, 113 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 152, 113 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 227, 87 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 291, 90 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 340, 102 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 384, 93 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 415, 78 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 471, 28 );
		++m_num_top_points;
		
		m_top_collision_points[m_num_top_points] = CIwFVec2( 481, 28 );
		++m_num_top_points;
	}
	
	m_num_bottom_points = 0;
	if( strcmp(bottom_bg, "level1_shore_bg_bottom") == 0 ) {
		// bottom
		m_bg_bottom_pos = CIwSVec2(0, 320 - 33);
		m_bottom_shore_collision_pre_check = check_collision_top_level1_shore_bottom;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 0, 293 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 148, 293 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 156, 300 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 192, 297 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 204, 304 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 257, 299 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 274, 311 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 276, 320 );
		++m_num_bottom_points;
	} else if( strcmp(bottom_bg, "level5_shore_bg_bottom") == 0 ) {
		m_bg_bottom_pos = CIwSVec2(0,320-148);
		m_bottom_shore_collision_pre_check = check_collision_top_level5_shore_bottom;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 0, 218 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 23, 233 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 22, 281 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 28, 289 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 70, 294 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 89, 285 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 117, 289 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 136, 300 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 166, 295 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 178, 240 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 177, 207 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 197, 185 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 246, 178 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 290, 187 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 313, 214);
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 335, 220 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 402, 220 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 429, 225 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 437, 233 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 479, 240 );
		++m_num_bottom_points;
	} else if( strcmp(bottom_bg, "level6_shore_bg_bottom") == 0 ) {
		m_bg_bottom_pos = CIwSVec2(0,320-75);
		m_bottom_shore_collision_pre_check = check_collision_top_level6_shore_bottom;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 0, 288 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 53, 276 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 68, 262 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 155, 254 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 181, 246 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 198, 245 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 217, 258 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 232, 286 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 259, 289 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 299, 284 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 308, 262 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 329, 261 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 357, 271 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 415, 270 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 441, 290 );
		++m_num_bottom_points;
		
		m_bottom_collision_points[m_num_bottom_points] = CIwFVec2( 480, 295 );
		++m_num_bottom_points;
	}
}

void GameplayBackground::Unload( ) 
{
	m_bg_bottom.Unload();
	m_bg_top.Unload(); 
	
	for( int i = 0; i < m_num_collision_objects; ++i ) {
		m_collision_objects[i].Unload();
	}
}

void GameplayBackground::Update( float dt )
{
	(void) dt; 
}

void GameplayBackground::Draw( )
{
	m_bg_top.Draw( m_bg_top_pos ); 
	m_bg_bottom.Draw( m_bg_bottom_pos );
	
	for( int i = 0; i < m_num_collision_objects; ++i ) {
		m_collision_objects[i].Draw();
	}
	
	/*
	// draw debug lines
	for( int i = 0; i < m_num_top_points -1; ++i ) {
		Iw2DDrawLine( 
			CIwSVec2( (int)m_top_collision_points[i].x, (int)m_top_collision_points[i].y ), 
            CIwSVec2( (int)m_top_collision_points[i+1].x, (int)m_top_collision_points[i+1].y ) ); 
    }
    
    for( int i = 0; i < m_num_bottom_points -1; ++i ) {
		Iw2DDrawLine( 
			CIwSVec2( (int)m_bottom_collision_points[i].x, (int)m_bottom_collision_points[i].y ), 
            CIwSVec2( (int)m_bottom_collision_points[i+1].x, (int)m_bottom_collision_points[i+1].y ) ); 
    }
    */
}

PolylineCollisionObject* GameplayBackground::AddPolylineCollisionObject()
{
	IL_ASSERT( m_num_collision_objects < MAX_COLLISION_OBJECTS, "to many collision objects in the scene" );
	++m_num_collision_objects;
	
	return &(m_collision_objects[m_num_collision_objects-1]);
}

bool GameplayBackground::CheckForCollision( const Ship &ship, CIwFVec2 *normal ) const
{
	// firstly check the top side
	// check if the ship is actually in a position to be colliding with the top 
	// background environment
	if( m_top_shore_collision_pre_check( ship ) ) {
		// the ship could possibly collide with the top environment
		// now we need to check the lines
		if( OBB::CheckForOverlapLines( ship.GetOBB(), m_top_collision_points, m_num_top_points, normal ) == true ) {
			return true;
		}
	}
	
	
	if( m_bottom_shore_collision_pre_check( ship ) ) {
	
		if( OBB::CheckForOverlapLines( ship.GetOBB(), m_bottom_collision_points, m_num_bottom_points, normal ) == true ) {
		
			// invert the normal, the normal is always pointing downwards
			*normal = -*normal;
		
			return true;
		}
	}
	
	float x = ship.GetPos().x;
	float y = ship.GetPos().y;
	
	// check every polyline object
	for( int i = 0; i < m_num_collision_objects; ++i ) {
	
		// do a first pre check if checking the actual lines is necessary
		CIwFVec2 object_pos = m_collision_objects[i].GetPos();
		float l = object_pos.x - 25; 
		float r = object_pos.x + m_collision_objects[i].GetWidth() + 25;
		float t = object_pos.y - 25;
		float b = object_pos.y + m_collision_objects[i].GetHeight() + 25;

		if( x > l && x < r && y < b && y > t ) {
			if( OBB::CheckForOverlapLines( ship.GetOBB(), m_collision_objects[i].GetCollisionPoints(), m_collision_objects[i].GetNumCollisionPoints(), normal ) == true ) {
			
				// invert the normal, the normal is always pointing towards the center
				*normal = -*normal;
			
				return true;
			}
		}
	}

	return false;
}